using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Hobba
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D boardTex;
        Texture2D redPiece;
        Texture2D redSelected;
        Texture2D blackSelected;
        bool[,] board;
        bool[,] player;
        bool playerTurn;
        bool[,] isSelected;
        bool pieceSelected = false;
        int selectedPieceRow, selectedPieceColumn, selectedPieceColumnOld, selectedPieceRowOld;

        MouseState mouseStateCurrent, mouseStatePrevious;
        Vector2 mousePositionCurrent;
        
        

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            
            this.graphics.PreferredBackBufferWidth = 1366;
            this.graphics.PreferredBackBufferHeight = 768;

            this.graphics.IsFullScreen = true;

        }

        protected bool Move(int y1,int x1,int y2,int x2,bool currentPlayer)
        {
            bool returnValue = false;
            int xsteps=0;
            int ysteps = 0;
            for (int i = 0; i < 8; i++)
                xsteps = board[x1, i] == true ? xsteps + 1 : xsteps;
            for (int i = 0; i < 8; i++)
                ysteps = board[i, y1] == true ? ysteps + 1 : ysteps;
            if ((x1 == x2 && y1 + xsteps == y2) || (x1 == x2 && y1 - xsteps == y2) ||
                (y1 == y2 && x1 + ysteps == x2) || (y1 == y2 && x1 - ysteps == x2))
            {
                returnValue=true;
            }
            if (returnValue)
            {
                board[x1, y1] = false;
                board[x2, y2] = true;
                player[x2, y2] = currentPlayer;
            }
            System.Diagnostics.Debug.WriteLine(ysteps);
            return returnValue;

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            board = new bool[8, 8];
            player = new bool[8, 8];
            isSelected = new bool[8, 8];
            for (int i = 0; i < 8; i += 7)
                for (int j = 1; j < 7; j++)
                {
                    board[i, j] = true;
                    player[i, j] = false;
                }
            for (int i = 1; i < 7; i++)
                for (int j = 0; j < 8; j+=7)
                {
                    board[i, j] = true;
                    player[i, j] = true;
                }
            for (int i = 0; i < 8; i++)
                for (int j = 0; j < 8; j ++)
                {
                    isSelected[i, j] = false;
                }

            this.IsMouseVisible = true;
            selectedPieceRow = -1;
            selectedPieceColumn = -1;
            selectedPieceRowOld = -1;
            selectedPieceColumnOld = -1;
            playerTurn = false;
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            boardTex = Content.Load<Texture2D>("board");
            redPiece = Content.Load<Texture2D>("red");
            redSelected = Content.Load<Texture2D>("redSelected");
            blackSelected = Content.Load<Texture2D>("blackSelected");
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            // Get current mouseState
            mouseStateCurrent = Mouse.GetState();
            
            // Left MouseClick
            if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
            {
                // TODO when left mousebutton clicked
                mousePositionCurrent.X = mouseStateCurrent.X;
                mousePositionCurrent.Y = mouseStateCurrent.Y;
                selectedPieceColumn = (int)(mousePositionCurrent.X - 34) / 90;
                selectedPieceRow = (int)(mousePositionCurrent.Y - 34) / 90;
                if (selectedPieceRow < 8 && selectedPieceRow < 8 && !pieceSelected)
                {
                    if (board[selectedPieceRow, selectedPieceColumn])
                    {
                        if (player[selectedPieceRow, selectedPieceColumn] == playerTurn)
                        {
                            isSelected[selectedPieceRow, selectedPieceColumn] = true;
                            pieceSelected = true;
                            selectedPieceColumnOld = selectedPieceColumn;
                            selectedPieceRowOld = selectedPieceRow;
                        }
                    }
                }
                System.Diagnostics.Debug.WriteLine(selectedPieceColumn);
                System.Diagnostics.Debug.WriteLine(selectedPieceRow);
                selectedPieceColumn = selectedPieceColumnOld;
                selectedPieceRow = selectedPieceRowOld;
                
            }

            // Right MouseClick
            if (mouseStateCurrent.RightButton == ButtonState.Pressed && mouseStatePrevious.RightButton == ButtonState.Released)
            {
                //TODO when right mousebutton clicked
                isSelected[selectedPieceRow, selectedPieceColumn] = false;
                pieceSelected = false;
            }

            mouseStatePrevious = mouseStateCurrent;



            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);
            spriteBatch.Begin();


            spriteBatch.Draw(boardTex, new Vector2(25.0f, 25.0f), null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            int row;
            int column;
            for (int i = 0; i < board.Length; i++)
            {
                row = i / 8;
                column = i % 8;

                if (board[row, column])
                {
                    spriteBatch.Draw(isSelected[row, column] ? (player[row, column] ? redSelected : blackSelected) : redPiece, new Vector2((34 + (90.0f * column)), (34 + (90.0f * row))), null, player[row, column] ? Color.White : (isSelected[row, column] ? Color.White : Color.Black), 0, Vector2.Zero, isSelected[row, column] ? 0.25f : 0.5f, SpriteEffects.None, 0);
                }
            }

            spriteBatch.End();


            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
